package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.sku.BaseSku;
import com.hoshiicloud.goods.param.ListSkuSpuParam;
import com.hoshiicloud.goods.param.sku.BaseSkuAddParam;
import com.hoshiicloud.goods.param.sku.BaseSkuQueryParam;
import com.hoshiicloud.goods.param.sku.BaseSkuUpdateParam;
import com.hoshiicloud.goods.param.sku.SkuBaseListParam;
import com.hoshiicloud.goods.service.BaseSkuService;
import com.hoshiicloud.goods.vo.ListSpuSkuVo;
import com.hoshiicloud.goods.vo.sku.BaseSkuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 基础商品表SKU 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/base-sku")
@Api(value = "基础商品表SKU", tags = {"基础商品表SKU"})
public class BaseSkuController {
    @Autowired
    private BaseSkuService baseSkuService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询基础商品表SKU
     */
    @ApiOperation(value = "分页查询基础商品表SKU")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BaseSkuQueryParam param) {
        try {
            log.info("分页查询基础商品表SKU");
            QueryWrapper<BaseSku> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(( null!= param.getSpuId() && 0 != param.getSpuId()), BaseSku::getSpuId, param.getSpuId())
                    .eq(StringUtils.isNotBlank(param.getSkuNo()), BaseSku::getSkuNo, param.getSkuNo())
                    .eq(StringUtils.isNotBlank(param.getBarCode()), BaseSku::getBarCode, param.getBarCode())
                    .eq(StringUtils.isNotBlank(param.getPacketCode()), BaseSku::getPacketCode, param.getPacketCode())
                    .eq((param.getIsStored() != null), BaseSku::getIsStored, param.getIsStored())
                    .eq(StringUtils.isNotBlank(param.getExtendProperty()), BaseSku::getExtendProperty, param.getExtendProperty())
                    .eq(StringUtils.isNotBlank(param.getExtendSpec()), BaseSku::getExtendSpec, param.getExtendSpec())
                    .eq(StringUtils.isNotBlank(param.getSkuTitle()), BaseSku::getSkuTitle, param.getSkuTitle())
                    .eq((param.getStatus() != null), BaseSku::getStatus, param.getStatus())
                    .eq((param.getWeight() != null), BaseSku::getWeight, param.getWeight())
                    .eq((param.getStrategy() != null), BaseSku::getStrategy, param.getStrategy())
                    .eq((param.getVendorSelf() != null), BaseSku::getVendorSelf, param.getVendorSelf())
                    .eq((param.getVendorId() != null), BaseSku::getVendorId, param.getVendorId())
                    .eq((param.getStockEnough() != null), BaseSku::getStockEnough, param.getStockEnough())
                    .eq((param.getDeleted() != null), BaseSku::getDeleted, param.getDeleted());
            IPage<BaseSku> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "BaseSkuQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(baseSkuService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询基础商品表SKU异常", e);
            return Response.failed("分页查询基础商品表SKU异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询基础商品表SKU
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询基础商品表SKU")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(baseSkuService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增基础商品表SKU")
    public Response save(@RequestBody @Valid BaseSkuAddParam param) {
        // Bean数据转换
        BaseSku entity;
        try {
            entity = BeanUtils.covertType(param, BaseSku.class);
        } catch (Exception e) {
            log.error("新增Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSkuService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改基础商品表SKU
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改基础商品表SKU")
    public Response update(@RequestBody @Valid BaseSkuUpdateParam param) {
        // Bean数据转换
        BaseSku entity;
        try {
            entity = BeanUtils.covertType(param, BaseSku.class);
        } catch (Exception e) {
            log.error("新增Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSkuService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除基础商品表SKU
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除基础商品表SKU")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(baseSkuService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询基础商品表SKU")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(baseSkuService.getByIds(ids, StringUtils.join(ids, "")));
    }


    @PostMapping("/saveBaseSku")
    @ApiOperation(value = "新增基础商品表SKU")
    public Response saveBaseSku(@RequestBody @Valid BaseSkuAddParam param) {
        // Bean数据转换
        BaseSku entity;
        try {
            entity = BeanUtils.covertType(param, BaseSku.class);
        } catch (Exception e) {
            log.error("新增Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSkuService.saveBaseSku(entity));
    }

    @PostMapping("/listByParam")
    @ApiOperation(value = "新增基础商品表SKU")
    public Response<List<BaseSkuVo>> listByParam(@RequestBody SkuBaseListParam param) {
        String key = param.toString();
        return Response.data(baseSkuService.listByParam(param,key));
    }

    @ApiOperation(value = "批量更新")
    @PostMapping("/updateBatch")
    public Response updateBatch(@RequestBody OmnipotentParam<BaseSku> param) {
        return Response.data(baseSkuService.updateBatch(param));
    }

    @ApiOperation(value = "批量查询")
    @PostMapping("/queryBatch")
    public Response queryBatch(@RequestBody List<OmnipotentWrapper> params) {
        return Response.data(baseSkuService.queryBatch(params));
    }

}
